# /usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2022/1/11 15:42
# @Author  : lt
# @Email   : lt@163.com
# @File    : add_sj_page.py
# @Software: PyCharm
'''添加商机界面'''
from selenium.webdriver.common.by import By
from page.KHGL_page.Base_Page import BasePage
from selenium.webdriver.support.select import Select
from time import sleep
class Add_sj_Page(BasePage):
    # 定位负责人输入框
    fuzeren_locator = (By.ID, 'owner_name')
    # 定位负责人表
    tbody_fuzeren_locator = (By.ID, 'd_content')

    # 定位负责人勾选确定按钮
    queren_fuzeren_locator = (By.XPATH, '/html/body/div[12]/div[3]/div/button[1]/span')

    # 定位客户输入框
    kehu_locator = (By.ID, 'customer_name')

    # 定位客户表
    tobdy_locator = (By.ID, 'datas')

    # 定位确认按钮
    queren_locator = (By.XPATH, '/html/body/div[10]/div[3]/div/button[1]/span')

    # 定位商机金额
    shangji_price_locator = (By.ID, 'total_price')

    # 定位商机名输入框
    shangji_name_locator = (By.ID, 'name')

    # 定位联系人
    lianxiren_locator = (By.ID, 'contacts_name')

    # 定位省地址
    adress_locator = (By.NAME, "contract_address['state']")

    # 定位城市
    city_locator = (By.NAME, "contract_address['city']")

    # 定位地区
    diqu_locator = (By.NAME, "contract_address['area']")

    # 定位商机类型输入框
    shangji_leixing_locator = (By.ID, 'type')

    # 定位客户状态
    kehu_zhuangtai_locator = (By.ID, 'status_id')

    # 定位商机来源
    shangji_laiyuan_locator = (By.ID, 'origin')

    # 定位赢单率
    yiingdanlv_locator = (By.ID, 'gain_rate')

    # 定位预计成交
    chengjiao_locator = (By.ID, 'estimate_price')

    # 预计下次联系时间
    next_staptime_locator = (By.ID, 'nextstep_time')

    # 定位下次联系内容
    next_nextstep_locator = (By.ID, 'nextstep')

    # 定位定位添加备注
    description_locator = (By.ID, 'description')

    # 定位添加产品按钮
    addchanpin_locator = (By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(12) > th > input')

    # 定位产品表
    chanpin_tobdy = (By.ID, 'data')

    # 定位确认按钮
    ok_locator = (By.CSS_SELECTOR,
                  'body > div:nth-child(22) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1) > span')

    # 定位保存按钮
    baocun_locator = (By.NAME, 'submit')

    # 添加成功
    tianchenggong_locator = (By.XPATH, '/html/body/div[5]/div[2]')

    # 点击负责人输入框
    def fuzeren_shurukuang(self):
        self.driver.find_element(*self.fuzeren_locator).click()
    def xuanze_fuzeren(self,fuzeren):
        tobdy_fuzeren = self.driver.find_element(*self.tbody_fuzeren_locator)
        trs1 = tobdy_fuzeren.find_elements(By.TAG_NAME, 'tr')
        for i in trs1:
            tds1 = i.find_elements(By.TAG_NAME, 'td')
            if tds1[1].text == fuzeren:
                tds1[0].click()
    #点击确定按钮
    def fuzeren_queren(self):
        self.driver.find_element(*self.queren_fuzeren_locator).click()
    # 点击客户输入框
    def kehu_shuurkuang(self):
        self.driver.find_element(*self.kehu_locator).click()
    # 选择客户
    def xuanze_kehu(self,kehu):
        tobdy_kehu = self.driver.find_element(*self.tobdy_locator)
        trs2 = tobdy_kehu.find_elements(By.TAG_NAME, 'tr')
        for i in trs2:
            tds2 = i.find_elements(By.TAG_NAME, 'td')
            if tds2[1].text == kehu:
                tds2[0].find_element(By.CSS_SELECTOR, '[name="customer"]').click()
    # 点击确认按钮
    def kehu_queren(self):
        self.driver.find_element(*self.queren_locator).click()
    # 输入上级金额
    def shangji_jine(self,shangji_jine):
        self.driver.find_element(*self.shangji_price_locator).send_keys(shangji_jine)
    # 输入商机名
    def shangji_name_shuru(self,shangji_name):
        self.driver.find_element(*self.shangji_name_locator).send_keys(shangji_name)
    # 输入联系人
    def lianxiren_shuru(self,lianxiren):
        self.driver.find_element(*self.lianxiren_locator).clear()
        self.driver.find_element(*self.lianxiren_locator).send_keys(lianxiren)
    # 输入省份
    def shengfen_adress(self,adress):
        self.driver.find_element(*self.adress_locator).send_keys(adress)
    # 输入城市
    def city_adress(self,city):
        self.driver.find_element(*self.city_locator).send_keys(city)
    # 输入地区
    def diqu_adress(self,diqu):
        self.driver.find_element(*self.diqu_locator).send_keys(diqu)
    # 输入商机类型
    def shangji_leixing(self,shangjileixing):
        Select(self.driver.find_element(*self.shangji_leixing_locator)).select_by_visible_text(shangjileixing)
    # 输入客户状态
    def kehu_zhuangtai(self,zhuangtai):
        Select(self.driver.find_element(*self.kehu_zhuangtai_locator)).select_by_visible_text(zhuangtai)
    # 输入商机来源
    def shangji_laiyuan(self,shangji_laiyuan):
        Select(self.driver.find_element(*self.shangji_laiyuan_locator)).select_by_visible_text(shangji_laiyuan)
    # 输入赢单率
    def yingdanlv_shuru(self,yingdanlv):
        self.driver.find_element(*self.yiingdanlv_locator).send_keys(yingdanlv)
    # 输入预计成交金额
    def yujijine(self,yujijiage):
        self.driver.find_element(*self.chengjiao_locator).send_keys(yujijiage)
    # 输入下次联系时间
    def next_time_shuru(self,next_time):
        self.driver.find_element(*self.next_staptime_locator).send_keys(next_time)
    # 输入下次联系内容
    def next_step_shuru(self,next_nextstep):
        self.driver.find_element(*self.next_nextstep_locator).send_keys(next_nextstep)
    # 输入备注
    def description_shuru(self,description):
        self.driver.find_element(*self.description_locator).send_keys(description)
    # 点击添加产品
    def add_chanpin(self):
        self.driver.find_element(*self.addchanpin_locator).click()
    # 输入要添加的产品
    def xuanze_chanpin(self,chanpin):
        tobdy_chanpin = self.driver.find_element(*self.chanpin_tobdy)
        trs3 = tobdy_chanpin.find_elements(By.TAG_NAME, 'tr')
        for i in trs3:
            tds3 = i.find_elements(By.TAG_NAME, 'td')
            if tds3[1].text == chanpin:
                tds3[0].find_element(By.CSS_SELECTOR, '[name="product_id"]').click()
                sleep(3)

    # 点击ok
    def tianjia_ok(self):
        self.driver.find_element(*self.ok_locator).click()
    # 点击保存
    def baocun_click(self):
        self.driver.find_element(*self.baocun_locator).click()
    # 断言
    def tianjia_succeful(self):
        text = self.driver.find_element(*self.tianchenggong_locator).text
        return text
